##### My (demuredemeanor) tmux config, a work in progress
# Uses tabstop=4; shiftwidth=4 tabs; foldmarker={{{,}}};
# https://notabug.org/demure/dotfiles/
# legacy repo http://github.com/demure/dotfiles
#
# Note to self: set=set-option; setw=set-window-option; setenv=set-environment;

### Main Settings ### {{{
## Fix glitch with underlined text in Terminal.app and hanging Prompt (iOS)
## From https://github.com/terlar/dotfiles/blob/master/tmux.conf
## Assume http://sourceforge.net/mailarchive/message.php?msg_id=30825085 is origin
set -ag terminal-overrides ",*:XT@"

#new-session                     ## tmux attach makes new, if not running
set -ga update-environment 'DISPLAY'    # Maybe fix DISPLAY?
set -g base-index 1             ## Window numbering starts at 1
set -g pane-base-index 1        ## Pane numbering starts at 1
setw -g automatic-rename on     ## Auto set window title
setw -g aggressive-resize on    ## Aggressive windows: For screen size, of diff connections, on diff windows
set -g default-terminal "tmux-256color"
#set -g default-terminal "screen-256color"
set -g history-limit 99999      ## Scroll back history
set -g display-panes-time 4000  ## Increase show pane time
set -g renumber-windows on      ## Make windows auto renumber
set -g lock-command vlock       ## Use vlock as console lock
set -g mouse on                 ## Mouse
### End Main Settings ### }}}

### Key Bindings ### {{{
unbind C-z                      ## Unbind C-z so I don't suspend, and free for bind
#unbind C-b                      ## Unbind C-b, so I can make C-z the prefix
set -g prefix C-z               ## Change prefix key to C-z
bind-key C-z last-window        ## Make <prefix>C-z go to last window
#unbind C-a
bind-key C-a send-prefix        ## Make <prefix>C-a send C-z literally

## Join windows: <prefix> s, <prefix> j
bind-key j command-prompt -p "join pane from:"  "join-pane -s '%%'"
bind-key s command-prompt -p "send pane to:"  "join-pane -t '%%'"

## URL grab: <prefix> u
#bind-key u capture-pane \; save-buffer /tmp/tmux-buffer \; new-window -n "urlview" '$SHELL -c "urlview < /tmp/tmux-buffer"'
bind-key u capture-pane \; save-buffer /tmp/tmux-buffer \; new-window -n "urlscan" '$SHELL -c "urlscan --single < /tmp/tmux-buffer || urlview < /tmp/tmux-buffer"'
bind-key U capture-pane \; save-buffer /tmp/tmux-buffer \; new-window -n "urlscan" 'urlscan --single --compact --run "tmux new-window tuir {}" < /tmp/tmux-buffer'

## Reload tmux.conf: <prefix> R
bind R source-file ~/.config/tmux/tmux.conf \; display-message "Config reloaded..."

## Lock screen
bind-key -T prefix X confirm-before -p "lock tmux? (y/n)" lock-session

## Better Bindings for Splitting
bind h split-window -h
bind v split-window -v

## Better Bindings for Splitting with current path
bind H split-window -hc "#{pane_current_path}"
bind V split-window -vc "#{pane_current_path}"
bind C new-window -c "#{pane_current_path}"

### Paste Buffer Bindings
bind b list-buffers  ## List paste buffers
#bind p paste-buffer  ## Paste from the top paste buffer
bind P choose-buffer ## Choose which buffer to paste from

    ### vi Mode for Scroll Back ### {{{
    setw -g mode-keys vi
    #unbind [
    bind Escape copy-mode
    unbind p
    bind p paste-buffer
    #bind-key -t vi-copy 'v' begin-selection
    #bind-key -t vi-copy 'y' copy-selection
    bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard'
    #bind-key -t vi-copy Escape cancel
    #bind-key -t vi-copy 'Y' copy-end-of-line
    #bind-key -t vi-copy 'V' select-line
    ### End vi Mode ### }}}

    ### Move Window Numbers ### {{{
    ## <prefix> Shift left/right
    # Inspired by https://superuser.com/questions/343572/how-do-i-reorder-tmux-windows
    bind-key S-Left swap-window -t -1
    bind-key S-Right swap-window -t +1
    ### End Window Numbers ### }}}
### End Key Bindings ### }}}

### Theming Settings ### {{{
## Terminal emulator window title
set -g set-titles on
set -g set-titles-string '#S:#I.#P #W'

## Notifying if other windows has activities
setw -g monitor-activity on
set -g visual-activity on

## Clock
setw -g clock-mode-colour green
setw -g clock-mode-style 24

    ### Status Bar ### {{{
    set -g status-bg black
    set -g status-fg white
    set -g status-interval 10

        ### Clean up host name ### {{{
        ## NOTE: %hidden needs 3.2+
        %if "#{==:#{host},moving-computer-of-doom}"
            ## Set for laptop
            %hidden S_HOST="doom"
        %elif "#{m:*-of-doom,#{host}}"
            ## Truncate personal host names
            %hidden S_HOST="#{s/-.*-doom//:host}"
        %else
            ## Limit hostnames to 10 char
            %hidden S_HOST="#{=10:host}"
        %endif
        ### End Clean up host name ### }}}

    ## Status Bar Spacing variable
    ## Initial Setting, if <100 col use condensed margin
    %if "#{e|>=:#{window_width},100}"
        %hidden SBS=" "
    %else
        %hidden SBS=""
    %endif

    #set-hook -g client-resized '
        #%if "#{e|>=:#{window_width},110}"
            #%hidden SBS="_"
        #%else
            #%hidden SBS="x"
        #%endif'


    ## Left Status Bar
    set -g status-left-length 15
    set -g status-left "#[fg=green,bg=#282A2E]${SBS}${S_HOST}${SBS}#[fg=#282A2E,bg=black]#[default]"

    ## Right Status Bar
    set -g status-right-length 60
    ## Show pane title|command, tmux session, datetime
    set -g status-right "\
#[fg=#282A2E,bg=black]#[fg=white,bg=#282A2E]#{?pane_in_mode,#[fg=yellow]${SBS}#{pane_mode}${SBS},}\
#[fg=#454A4F,bg=#282A2E]#[fg=white,bg=#454A4F]${SBS}#{?#{!=:#{pane_title},#{host}},#{=15;s/ .*//:pane_title},#{=15:pane_current_command}}${SBS}\
#[fg=#282A2E,bg=#454A4F]#[fg=white,bg=#282A2E]#{?#{!=:#{session_name},#{s/-of-doom//:host}},#{l:${SBS}}#{=10:session_name}#{l:${SBS}},}\
#[fg=#454A4F,bg=#282A2E]#[fg=white,bg=#454A4F]${SBS}#(while [ -e ~/.config/tmux/battery_check.sh ]; do ~/.config/tmux/battery_check.sh; sleep 300; done)${SBS}\
#[fg=#282A2E,bg=#454A4F]#[fg=cyan,bold,bg=#282A2E]${SBS}%Y-%m-%d${SBS}${SBS}%H:%M${SBS}#[default]"

    ## Window Status Bar
    setw -g window-status-format "${SBS}#[fg=cyan,dim]#I#[default] #W${SBS}"
    setw -g window-status-separator ''
    ## Highlighting the active window in status bar
    setw -g window-status-current-format '#[fg=black,bg=blue]#[bg=blue,fg=cyan]#I#[bg=blue,fg=white] #W#[bold] #F#[none]#[fg=blue,bg=black]#[default]'
    ## Inactive windows
    setw -g window-status-last-style 'underscore'
    setw -g window-status-activity-style 'fg=blue,bold'
    setw -g window-status-bell-style 'fg=red'
    ### End Status Bar ### }}}

    ### Message/Message Command ### {{{
    set -g message-style 'fg=white,bg=cyan,bright'
    set -g message-command-style 'fg=white,bg=cyan,bright'
    ### End Message ### }}}

    ### Pane Colors ### {{{
    set -g pane-border-style 'fg=white'
    set -g pane-active-border-style 'fg=cyan'
    set -gq pane-border-lines double
    #set -g pane-border-status off
    ### End Pane Colors ### }}}
### End Theming Settings ### }}}
